<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- js对象的继承 -->
    <script>
        // 传统形式-->原型链
        // 整体连成了一个链，想继承的继承了，不想继承的也继承了
     // Grand.prototype.lastName = 'Ji';
     // function Grand() {
     // }
     // var grand = new Grand();
     // Father.prototype = grand;
     // function Father() {
     //     this.name = 'hehe';
     // }
     // var father = new Father();
     // Son.prototype = father;
     // function Son() {
     // }
     // var son = new Son();
    </script>


    <!-- <script>
        // 借用构造函数
        // 不算真正的继承
        function Person(name, age, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }

        function Student(name, age, sex, grade){
            Person.call(this, name, age, sex);
            this.grade = grade;
        }

        var student = new Student();
    </script> -->

    <script>

        // 共享原型
        // 不能随便改动自己的原型

        Father.prototype.lastName = 'deng';
        function Father() {

        }
        function Son() {

        }
        // Son.prototype = Father.prototype;
        inherit(Son, Father);//先执行继承函数，在使用生产son对象

        Son.prototype.sex = 'male';
        // 共用一个原型，修改Son的原型，Father也会变

        var son = new Son(); // son继承了father的原型
        var father = new Father();
        console.log('son', son);
        console.log('father', father);


        function inherit(Target, Origin) {
            Target.prototype = Origin.prototype;
        }



        // 圣杯模式
        //修改Son的原型不会影响Father的原型
        function inherit(Target, Origin) {
            function F() {}
            F.prototype = Origin.prototype;
            Target.prototype = new F();
            // 到此为止，还差一步
            Target.prototype.constructor = Target;
            Target.prototype.uber = Origin.prototype;
        }

        //高端的写法 （背过，相当重要）
        var inherit = (function(){
            var F = function() {};
            return function(Target, Origin) {
                F.prototype = Origin.prototype;
                Target.prototype = new F();
                Target.prototype.constructor = Target;
                Target.prototype.uber = Origin.prototype;
            }
        }());


    </script>


</body>
</html>